package com.sysman.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sysman.HttpTools.DataAnalyze;
import com.sysman.HttpTools.HttpRequest;

/** 
* @author  LTT-AH-ZY
* @date 创建时间：2017年12月13日 下午2:28:07 
* @version 1.0 
* @parameter 参数及其意义
* @since 最早使用该方法/类/接口的JDK版本 
* @since  
* @return  
*/
@Controller
@RequestMapping(value="/Main/Paymoney")
public class PaymoneyController {
	/**
	 * 付款给医生界面的初始化
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/payforDoc")
	public ModelAndView payforDoc()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("View/PayMoneyMan/PayforDoctor");
		return model;
	}
	/**
	 * 异步获取需要退款的医生列表
	 * @param limit
	 * @param offset
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/listDoctorFundAjax")
	public @ResponseBody Map<String,Object> listDoctorFundAjax(Integer limit,Integer offset,String docname,String docloginphone,String docalipayaccount,HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		String param = "adminloginid="+adminloginid+"&limit="+limit+"&offset="+offset+"&docname="+docname+"&docloginphone="+docloginphone+"&docalipayaccount="+docalipayaccount;
		System.out.println("输出这个参数："+param);
		Map<String,Object> result = new HashMap<>();
		List<Map<String,Object>> list = new ArrayList<>();
		String jsoncode = "";
		String jsonmsg = "";
		Integer total=0;
		try {
			JSONObject json = HttpRequest.sendPost("listdoctortofund", param);
			jsoncode = json.get("code").toString();
			jsonmsg = json.get("msg").toString();
			if("100".equals(jsoncode)) {
				try {
					if(json.get("data").toString()!="null") {
						JSONObject jsondata=json.getJSONObject("data");
						JSONArray jsonarrayrows = jsondata.getJSONArray("rows");
						total = Integer.parseInt(jsondata.get("total").toString());
						list=DataAnalyze.DcotorfundJsonToList(jsonarrayrows);
					}
				} catch (Exception e) {
					System.out.println("出错信息是："+e.getMessage());
					jsoncode="-200";
					jsonmsg="数据解析错误";
				}
			}
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			jsoncode="-100";
			jsonmsg="数据传输错误";
		}
		
		result.put("code", jsoncode);
		result.put("msg", jsonmsg);
		result.put("rows", list);
		result.put("total", total);
		return result;
	}
	/**
	 * 打款给医生
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/Fundtodoctor")
	public @ResponseBody Map<String,Object> Fundtodoctor(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		Integer docloginid=Integer.parseInt(request.getParameter("docloginid"));
		String param = "adminloginid="+adminloginid+"&docloginid="+docloginid;
		
		Map<String,Object> result = new HashMap<String,Object>();
		String code="";
		String msg="";
		try {
			JSONObject json = HttpRequest.sendPost("fundtodoctor", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			code="-100";
			msg="数据传输错误";
		}
		result.put("code", code);
		result.put("msg", msg);
		return result;
	}
	
	/**
	 * 付款给医生界面的初始化
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/payforHosp")
	public ModelAndView payforHosp()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("View/PayMoneyMan/PayforHospital");
		return model;
	}
	/**
	 * 异步获取需要退款的医生列表
	 * @param limit
	 * @param offset
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/listHospitalFundAjax")
	public @ResponseBody Map<String,Object> listHospitalFundAjax(Integer limit,Integer offset,String hospname,String hosploginphone,String hospalipayaccount,HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		String param = "adminloginid="+adminloginid+"&limit="+limit+"&offset="+offset+"&hospname="+hospname+"&hosploginphone="+hosploginphone+"&hospalipayaccount="+hospalipayaccount;
		System.out.println("输出这个参数："+param);
		Map<String,Object> result = new HashMap<>();
		List<Map<String,Object>> list = new ArrayList<>();
		String jsoncode = "";
		String jsonmsg = "";
		Integer total=0;
		try {
			JSONObject json = HttpRequest.sendPost("listhospitaltofund", param);
			jsoncode = json.get("code").toString();
			jsonmsg = json.get("msg").toString();
			if("100".equals(jsoncode)) {
				try {
					if(json.get("data").toString()!="null") {
						JSONObject jsondata=json.getJSONObject("data");
						JSONArray jsonarrayrows = jsondata.getJSONArray("rows");
						total = Integer.parseInt(jsondata.get("total").toString());
						list=DataAnalyze.HospitalfundJsonToList(jsonarrayrows);
					}
				} catch (Exception e) {
					System.out.println("出错信息是："+e.getMessage());
					jsoncode="-200";
					jsonmsg="数据解析错误";
				}
			}
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			jsoncode="-100";
			jsonmsg="数据传输错误";
		}
		
		result.put("code", jsoncode);
		result.put("msg", jsonmsg);
		result.put("rows", list);
		result.put("total", total);
		return result;
	}
	/**
	 * 打款给医院
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/Fundtohospital")
	public @ResponseBody Map<String,Object> Fundtohospital(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		Integer hosploginid=Integer.parseInt(request.getParameter("hosploginid"));
		String param = "adminloginid="+adminloginid+"&hosploginid="+hosploginid;
		
		Map<String,Object> result = new HashMap<String,Object>();
		String code="";
		String msg="";
		try {
			JSONObject json = HttpRequest.sendPost("fundtohospital", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			code="-100";
			msg="数据传输错误";
		}
		result.put("code", code);
		result.put("msg", msg);
		return result;
	}
	/**
	 * 付款给医生界面的初始化
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/payforPatient")
	public ModelAndView payforPatient()throws Exception{
		ModelAndView model = new ModelAndView();
		model.setViewName("View/PayMoneyMan/PayforPatient");
		return model;
	}
	/**
	 * 异步获取需要退款的医生列表
	 * @param limit
	 * @param offset
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/listPatientFundAjax")
	public @ResponseBody Map<String,Object> listPatientFundAjax(Integer limit,Integer offset,String username,String userloginphone,String useralipayaccount,String startdatetime,String enddatetime,HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		String param = "adminloginid="+adminloginid+"&limit="+limit+"&offset="+offset+"&username="+username+"&userloginphone="+userloginphone+"&useralipayaccount="+useralipayaccount+"&startdatetime="+startdatetime+"&enddatetime="+enddatetime;
		System.out.println("输出这个参数："+param);
		Map<String,Object> result = new HashMap<>();
		List<Map<String,Object>> list = new ArrayList<>();
		String jsoncode = "";
		String jsonmsg = "";
		Integer total=0;
		try {
			JSONObject json = HttpRequest.sendPost("listordertofund", param);
			jsoncode = json.get("code").toString();
			jsonmsg = json.get("msg").toString();
			if("100".equals(jsoncode)) {
				try {
					if(json.get("data").toString()!="null") {
						JSONObject jsondata=json.getJSONObject("data");
						JSONArray jsonarrayrows = jsondata.getJSONArray("rows");
						total = Integer.parseInt(jsondata.get("total").toString());
						list=DataAnalyze.PatientfundJsonToList(jsonarrayrows);
					}
				} catch (Exception e) {
					System.out.println("出错信息是："+e.getMessage());
					jsoncode="-200";
					jsonmsg="数据解析错误";
				}
			}
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			jsoncode="-100";
			jsonmsg="数据传输错误";
		}
		
		result.put("code", jsoncode);
		result.put("msg", jsonmsg);
		result.put("rows", list);
		result.put("total", total);
		return result;
	}
	/**
	 * 退款给病人
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/ReFundtouser")
	public @ResponseBody Map<String,Object> ReFundtouser(HttpServletRequest request)throws Exception{
		HttpSession session = request.getSession();
		Integer adminloginid = (Integer)session.getAttribute("adminloginid");
		Integer userorderid=Integer.parseInt(request.getParameter("userorderid"));
		String param = "adminloginid="+adminloginid+"&userorderid="+userorderid;
		
		Map<String,Object> result = new HashMap<String,Object>();
		String code="";
		String msg="";
		try {
			JSONObject json = HttpRequest.sendPost("fundtouser", param);
			code=json.get("code").toString();
			msg=json.get("msg").toString();
		} catch (Exception e) {
			System.out.println("出错了，错误信息是："+e.getMessage());
			code="-100";
			msg="数据传输错误";
		}
		result.put("code", code);
		result.put("msg", msg);
		return result;
	}
}
